﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace PMSSys
{
    public partial class PatientView : Form
    {
        private bool isModified = false;

        public PatientView(string p_id, patientController pccontroller)
        {
            InitializeComponent();

            ppprocessor = pccontroller;
            pObject = new Patient();
            if (systemAdding = String.IsNullOrEmpty(p_id))
            {
                exportButton.Enabled = false;
            }
            else 
            {
                pObject.PatientID = int.Parse(p_id);
                ppprocessor.objectFiller(pObject);
                PLName.Text = pObject.LName;
                PMname.Text = pObject.MName;
                PFName.Text = pObject.FName;
                PBDate.Value = pObject.Birthday;
                if (pObject.Sex == "M")
                    radioButton1.Checked = true;
                else
                    radioButton2.Checked = true;
                PAddress.Text = pObject.Address;
                PTelNo.Text = pObject.TelNo;
                PCelNo.Text = pObject.CelNo;
                PFatherName.Text = pObject.FatherName;
                PFOccupation.Text = pObject.FatherOccu;
                PMotherName.Text = pObject.MotherName;
                PMOccupation.Text = pObject.MotherOccu;
                PSiblingsCount.Text = pObject.SiblingCount.ToString();
                PAnnualIncome.Text = pObject.AnnualInc.ToString();
                PReferrer.Text = pObject.Refferal;
                isModified = false;
            }


        }
        private Boolean systemAdding;
        private patientController ppprocessor;
        private Patient pObject;

        private void icdButton_Click(object sender, EventArgs e)
        {
            InitialClinicalDataView newWindow = new InitialClinicalDataView();
            newWindow.ShowDialog();
        }

        private void treatmentsButton_Click(object sender, EventArgs e)
        {
            TreatmentListViewer newWindow = new TreatmentListViewer(ppprocessor, pObject);
            newWindow.ShowDialog();
        }

        private void followupsButton_Click(object sender, EventArgs e)
        {
            FollowUpView newWindow = new FollowUpView(ppprocessor, pObject);
            newWindow.ShowDialog();
        }

        private void bMTButton_Click(object sender, EventArgs e)
        {
            BoneMarrowList newWindow = new BoneMarrowList(ppprocessor, pObject);
            newWindow.ShowDialog();
        }

        private void paymentsButton_Click(object sender, EventArgs e)
        {
            PaymentListView newWindow = new PaymentListView(ppprocessor, pObject);
            newWindow.ShowDialog();
        }

        private void exportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Microsoft Word Document|*.docx";
            saveFileDialog1.Title = "Save Exported Record to";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {

                Object OverloadParams = System.Reflection.Missing.Value;

                Object oTrue = true;
                Object oFalse = false;

                Word.Application CreateWord = new Word.Application();
                Word.Document AssembleDoc = new Word.Document();

                CreateWord.Visible = false;
                Object SavePath = System.Windows.Forms.Application.StartupPath + "Insert Template Filename here";
                Object FileSavePath = "C:\\" + saveFileDialog1.FileName + ".doc";

                AssembleDoc = CreateWord.Documents.Add(ref SavePath, ref OverloadParams, ref OverloadParams, ref OverloadParams);
                AssembleDoc.SaveAs(ref FileSavePath, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams, ref OverloadParams);

                int iReadFields;
                iReadFields = 0;

                foreach (Word.Field myMergeField in AssembleDoc.Fields)
                {
                    iReadFields++;
                    Word.Range rangeCode = myMergeField.Code;
                    String StreamText = rangeCode.Text;

                    if (StreamText.StartsWith(" MERGEFIELD"))
                    {
                        Int32 endMerge = StreamText.IndexOf("\\");
                        Int32 fieldNameLength = StreamText.Length - endMerge;
                        String fieldName = StreamText.Substring(11, endMerge - 11);
                        fieldName = fieldName.Trim();

                        if (fieldName == "name of variable in template")
                        {
                            myMergeField.Select();

                        }


                    }

                }

                //code for saving and export goes here
                //pObject.FatherName;

            }
        }

        private void anyvalue_changed(object sender, EventArgs e)
        {
            isModified = true;
        }

        private void PatientView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isModified)
            {
                DialogResult res = uiUtility.confirmSaveDialog();
                if (res != DialogResult.Cancel)
                {
                    if (res == DialogResult.Yes)
                    {
                        if (PFName.Text == string.Empty || PLName.Text == string.Empty)
                        {
                            MessageBox.Show("Please enter patient name", "No name found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            e.Cancel = true;
                        }
                        else
                        {
                            if (systemAdding)
                            {
                                addPatientGuiChecker();
                                addHandler();
                            }
                            else
                            {
                                addPatientGuiChecker();
                                ///save code goes here
                            }
                        }
                    }

                }
                else
                    e.Cancel = true;

            }
        }
        private void addPatientGuiChecker()
        {
            DateTime dateNow = DateTime.Now;
            int bDateRes = DateTime.Compare(DateTime.Parse(PBDate.Value.ToString()), dateNow);
            
            if (bDateRes >= 0)
            {
                MessageBox.Show("Please Enter a Validate Date");
            }
            if (radioButton1.Checked == false && radioButton2.Checked == false)
            {
                MessageBox.Show("Select a gender");
            }
        }
        private String[] addHandler()
        {
            String[] result = null;
            Patient pasyente = new Patient();

            pasyente.LName = PLName.Text;
            pasyente.FName = PFName.Text;
            pasyente.MName = PMname.Text;
            pasyente.Birthday = DateTime.Parse(PBDate.Value.ToString());
            if (radioButton1.Checked)
            {
                pasyente.Sex = "M";
            }
            else if (radioButton2.Checked)
            {
                pasyente.Sex = "F";
            }
            pasyente.Address = PAddress.Text;
            pasyente.TelNo = PTelNo.Text;
            pasyente.CelNo = PCelNo.Text;
            pasyente.FatherName = PFatherName.Text;
            pasyente.FatherOccu = PFOccupation.Text;
            pasyente.MotherName = PMotherName.Text;
            pasyente.MotherOccu = PMOccupation.Text;
            pasyente.SiblingCount = Int32.Parse(PSiblingsCount.Text);
            pasyente.AnnualInc = Int32.Parse(PAnnualIncome.Text);
            pasyente.Refferal = PReferrer.Text;

            String tempresult = ppprocessor.add(pasyente);
            MessageBox.Show(tempresult);
            return result;
        }
        private void closeLogic(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PatientView_Load(object sender, EventArgs e)
        {
            ToolTip t1 = uiUtility.tooltips();
            t1.SetToolTip(this.icdButton, "Initial Clinical Data");
            t1.SetToolTip(this.treatmentsButton, "Treatment & Medication Plan");
            t1.SetToolTip(this.followupsButton, "Follow-Up");
            t1.SetToolTip(this.bMTButton, "Bone Marrow Tests");
            t1.SetToolTip(this.paymentsButton, "Payments");
            t1.SetToolTip(this.exportButton, "Export Record");
            t1.SetToolTip(this.button6, "Back");
        }





    }
}
